package beerAgent;

import evolutionaryLoop.selectionMechanisme.SelectionMechanism;
import evolutionaryLoop.selectionMechanisme.SigmaSelection;
import evolutionaryLoop.selectionMechanisme.Tournament;
import evolutionaryProblems.Phenotype;
import gui.SettingPanel;

import java.awt.GridBagConstraints;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;

public class BeerSettingPanel extends SettingPanel{

	private static final long serialVersionUID = 1L;
	private JLabel hiddenLayers;
	private JTextField layers;
	private JCheckBox horizontalSpeed;
	private JLabel nofMapsLabel;
	private JTextField nofMapsTextField;
	private JLabel dynamicMapLabel;
	private JCheckBox dynamicMap;
	private JLabel avoidenceLabel;
	private JCheckBox avoidence;
	private JTextField widthTextField;
	private JTextField heightTextField;
	private JToggleButton sigmaToggleButton;
	private JToggleButton tournamentToggleButton; 
	
	public BeerSettingPanel(BeerProblem problem, BeerParameters parameters) {
		super(problem,parameters); 
	}
	
	@Override
	protected void initComponents(){
		super.initComponents(); 

		
		hiddenLayers = new JLabel("Hidden layers");
		addComponent(hiddenLayers, 0, 5); 
		addHiddenLayerField();
		
		nofMapsLabel = new JLabel("nof Maps"); 
		addComponent(nofMapsLabel, 2, 5);
		
		nofMapsTextField = new JTextField(); 
		nofMapsTextField.setColumns(10); 
		addComponent(nofMapsTextField, 3, 5); 
		
		JLabel widthLabel = new JLabel("Width"); 
		addComponent(widthLabel, 0, 6);
		
		widthTextField = new JTextField(); 
		widthTextField.setColumns(10); 
		addComponent(widthTextField, 1, 6); 
		
		JLabel heightLabel = new JLabel("Height"); 
		addComponent(heightLabel, 2, 6);
		
		heightTextField = new JTextField(); 
		heightTextField.setColumns(10); 
		addComponent(heightTextField, 3, 6); 
		
		hiddenLayers = new JLabel("Horizontal speed");
		addComponent(hiddenLayers, 0, 7);
		
		horizontalSpeed = new JCheckBox(); 
//		horizontalSpeed.addActionListener(new HorizontalSpeedActionListner()); 
		addComponent(horizontalSpeed, 1, 7); 
		
		dynamicMapLabel = new JLabel("Dynamic map");
		addComponent(dynamicMapLabel, 2, 7);
		
		dynamicMap = new JCheckBox(); 
//		horizontalSpeed.addActionListener(new HorizontalSpeedActionListner()); 
		addComponent(dynamicMap, 3, 7); 
		
		avoidenceLabel = new JLabel("Avoidance");
		addComponent(avoidenceLabel, 0, 8);
		
		avoidence = new JCheckBox(); 
//		horizontalSpeed.addActionListener(new HorizontalSpeedActionListner()); 
		addComponent(avoidence, 1, 8); 
		
		
		ButtonGroup buttonGroup = new ButtonGroup(); 
		sigmaToggleButton = new JToggleButton("Sigma"); 
		tournamentToggleButton = new JToggleButton("Tournament"); 
		buttonGroup.add(sigmaToggleButton);
		buttonGroup.add(tournamentToggleButton); 
		addComponent(sigmaToggleButton, 2, 8); 
		addComponent(tournamentToggleButton, 3, 8);
		
	}

	private void addHiddenLayerField() {
		layers = new JTextField();
		layers.setColumns(10);
		GridBagConstraints constraint = new GridBagConstraints(); 
		constraint.gridx = 1; 
		constraint.gridy = 5; 
		constraint.gridwidth = 3; 
		constraint.anchor = GridBagConstraints.WEST; 
		this.add(layers, constraint);
	}
	
	@Override
	protected void readParameters(){
		super.readParameters(); 
		BeerParameters parameters = ((BeerParameters) this.parameters); 
		parameters.setNetworkLayout(setLayers());
		parameters.setHorizontalSpeed(horizontalSpeed.isSelected()); 
		parameters.setDynamicMap(dynamicMap.isSelected()); 
		parameters.setAvoidence(avoidence.isSelected());
		parameters.setNofMaps(Integer.parseInt(nofMapsTextField.getText())); 
		parameters.setWidth(Integer.parseInt(widthTextField.getText()));
		parameters.setHeight(Integer.parseInt(heightTextField.getText()));
		
		if (sigmaToggleButton.isSelected()) {
			parameters.setParenetSelectionMechanism(new SigmaSelection()); 
		} else {
			parameters.setParenetSelectionMechanism(new Tournament(0.05, 5, new SigmaSelection())); 
		}
	}
	
	@Override
	public void showDefaultParameters(){
		super.showDefaultParameters(); 
		BeerParameters parameters = ((BeerParameters)this.parameters); 
		layers.setText(getHiddenLayers());
		horizontalSpeed.setSelected(parameters.getHorizontalSpeed()); 
		nofMapsTextField.setText(Integer.toString(parameters.getNofMaps())); 
		avoidence.setSelected(parameters.getAvoidence()); 
		dynamicMap.setSelected(parameters.getDynamicMap()); 
		widthTextField.setText(Integer.toString(parameters.getWidth())); 
		heightTextField.setText(Integer.toString(parameters.getHeight())); 
		
//		System.out.println(parameters.getFitnessFunction());
		if (parameters.getParenetSelectionMechanism() instanceof SigmaSelection) {
			sigmaToggleButton.setSelected(true); 
		} else {
			tournamentToggleButton.setSelected(true); 
		}
	}
	
	private String getHiddenLayers() {
		String s = "";
		int[] layout = ((BeerParameters) parameters).getNetworkLayout();
		
		if (layout.length == 2)
			return "";
		
		for (int i = 1; i < layout.length-1; i++) {
			s += Integer.toString(layout[i])+ ",";
		}
		s = s.substring(0, s.length()-1);
		return s;
	}
	
	private int[] setLayers() {
		BeerParameters parameters = (BeerParameters) this.parameters; 
		String hidden = layers.getText().replaceAll("\\s", "");
		String returnString = "";
		String input = Integer.toString(parameters.getNetworkLayout()[0]);
		String output = Integer.toString(parameters.getNetworkLayout()[parameters.getNetworkLayout().length-1]);

		if (hidden.length() == 0)
			returnString = input + "," + output;
		else returnString = input + "," + hidden + "," + output;

		String[] layersString = returnString.split(",");
		int[] layersInt = new int[layersString.length];
		for (int i = 0; i < layersString.length; i++) {
			if (! layersString[i].equals("") )
					layersInt[i] = Integer.parseInt(layersString[i]);
		}
//		System.out.println("Setlayers:" + Arrays.toString(layersInt));
		return layersInt;
	}
	
}
